<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="stuName">
        <el-input
          v-model="queryParams.stuName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="学号" prop="stuId">
        <el-input
          v-model="queryParams.stuId"
          placeholder="请输入学号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班级" prop="stuClassId">
         <el-select
            v-model="queryParams.claId"
            placeholder="请选择班级"
            style="width: 240px"
          >
          <el-option label="全部" value=""></el-option>
          <el-option v-for="item in classList" :label="item.grade + '级' + item.claName" :value="item.id"></el-option>

          </el-select>

      </el-form-item>
    
      
      <el-form-item label="学期" prop="semName">
        <el-select v-model="queryParams.semId" placeholder="请选择学期">
          <el-option label="全部" :value="null"></el-option>
          <el-option v-for="item in semesterList" :key="item.id" :label="item.semName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
    
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['business:points:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:points:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:points:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['business:points:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="stuName" />
      <el-table-column label="学号" align="center" prop="stuId" />
      <el-table-column label="专业" align="center" prop="stuSpecialities" />
      <el-table-column label="班级" align="center" :formatter="rows => rows.stuClassManagement.grade + '级' + rows.stuClassManagement.claName + '班'"/>
        <el-table-column label="积分" align="center" prop="stuScore" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <!-- <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:points:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['business:points:remove']">删除</el-button> -->
          
          <el-button link type="primary" icon="Document" @click="handleShow(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改积分记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="pointsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学期名称" prop="semName">
          <el-select v-model="form.semName" placeholder="请选择学期">
          <el-option v-for="item in semesterList" :key="item.id" :label="item.semName" :value="item.semName"></el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="学生唯一标识" prop="stuId">
          <el-input v-model="form.stuId" placeholder="请输入学生唯一标识" />
        </el-form-item>
        <el-form-item label="积分修改原因" prop="upPoints">
          <el-input v-model="form.upPoints" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="积分修改值" prop="upScore">
          <el-input v-model="form.upScore" placeholder="请输入积分修改值" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Points">
import { listPoints ,listStudentPoints , getPoints, delPoints, addPoints, updatePoints } from "@/api/business/points";
import { listClassManagement } from "@/api/business/classManagement";

import { listSemester } from "@/api/business/semester"

const { proxy } = getCurrentInstance();

import { ref, reactive, onMounted } from "vue";

// onMounted(async ()=>{
//   // 获取学期,存到学期列表中
//   let data = await listSemester();
//   semesterList.value = data.rows
//   console.log("获取学期列表成功:",semesterList);
// })

onMounted(async () => {
  listSemester().then(response => {
    semesterList.value = response.rows
  })
 let res = await listStudentPoints()
  console.log("res:",res);
  studentList.value = res.rows


  const classData = await listClassManagement();
  classList.value = classData.rows

})

const pointsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const semesterList = ref([]);
const studentList = ref([]);
const classList = ref([]);


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    id: null,
    semName: null,
    stuId: null,
    upPoints: null,
    upScore: null,
    createTime: null,
    updateTime: null
  },
  rules: {
    semName: [
      { required: true, message: "学期名称不能为空", trigger: "blur" }
    ],
    stuId: [
      { required: true, message: "学生唯一标识不能为空", trigger: "blur" }
    ],
    upPoints: [
      { required: true, message: "积分修改原因不能为空", trigger: "blur" }
    ],
    upScore: [
      { required: true, message: "积分修改值不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

const handleShow = (row) => {
  console.log("row:",row);
 
}

/** 查询积分记录列表 */
function getList() {
  loading.value = true;

  listStudentPoints(queryParams.value).then(response => {
    studentList.value = response.rows
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    semName: null,
    stuId: null,
    upPoints: null,
    upScore: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("pointsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加积分记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getPoints(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改积分记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["pointsRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updatePoints(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPoints(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除积分记录编号为"' + _ids + '"的数据项？').then(function() {
    return delPoints(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('business/points/export', {
    ...queryParams.value
  }, `points_${new Date().getTime()}.xlsx`)
}

getList();

</script>
